﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GtsServer.Model;
using GtsServer.Common;
using GtsServer.Manage.Controller;

namespace GtsServer.Manage.IISPage.Ajax
{
    /// <summary>
    /// JournalAjax 的摘要说明
    /// </summary>
    public class JournalAjax : IHttpHandler
    {
        JournalController Controller = new JournalController();
        public void ProcessRequest(HttpContext context)
        {
            if (context.Request["action"] != null)
            {
                string type = string.Empty;
                type = context.Request["action"];

                switch (type)
                {
                    case "GetList":
                        GetList(context);
                        break;
                    case "GetListBySelect":
                        GetListBySelect(context);
                        break;
                    default:
                        break;
                }
            }
        }
        private void GetList(HttpContext context)
        {
            int pageIndex = Convert.ToInt32(context.Request["pageIndex"]);
            int pageSize = Convert.ToInt32(context.Request["pageSize"]);

            string Pid = context.Request["Pid"];
            string vesion = context.Request["vesion"];
            string potion = context.Request["potion"];
            string IMSI = context.Request["IMSI"];
            string StartTime = context.Request["StartTime"];
            string EndTime = context.Request["EndTime"];
            string userAccount = context.Request["userAccount"];

            string strWhere = string.Empty;
            strWhere += " 1=1 ";
            if (Pid == "-请选择PID-")
            {
                Pid = "";
            }
            if (potion == "-请选择联网位置-")
            {
                potion = "";
                return;
            }
            if (vesion == "-选择版本号-")
            {
                vesion = "";
            }
            if (!string.IsNullOrEmpty(Pid))
            {
                strWhere += " and Pid=" + Pid;
            }
            if (!string.IsNullOrEmpty(vesion))
            {
                strWhere += " and Vesion='" + vesion + "'";
            }
            if (!string.IsNullOrEmpty(potion))
            {
                strWhere += " and Position='" + potion + "'";
            }
            if (!string.IsNullOrEmpty(IMSI))
            {
                strWhere += " and IMSI='" + IMSI + "'";
            }
            if (!string.IsNullOrEmpty(StartTime))   
            {
                strWhere += " and RequestTime>'" + Convert.ToDateTime(StartTime) + "'";
            }
            if (!string.IsNullOrEmpty(EndTime))
            {
                strWhere += " and RequestTime< '" + Convert.ToDateTime(EndTime) + "'";
            }
            if (!string.IsNullOrEmpty(userAccount))
            {
                strWhere += " and UserAccount= '" + userAccount + "'";
            }

            int count = 0;
            List<JournalReportMould> list = Controller.GetList(pageIndex, pageSize,potion, out count, strWhere);
            for (int i = 0; i < list.Count; i++)
            {
                list[i].UpStream = Microsoft.JScript.GlobalObject.encodeURIComponent(list[i].UpStream);
                list[i].DownStream = Microsoft.JScript.GlobalObject.encodeURIComponent(list[i].DownStream);
            }
            string json = JsonHelper.ListToJson<JournalReportMould>(list, count.ToString());
            context.Response.Write(json);
        }
        private void GetListBySelect(HttpContext context)
        {
            List<PositionInfoModel> list = Controller.GetListBySelect();
            string json = JsonHelper.ListToJson<PositionInfoModel>(list);
            context.Response.Write(json);
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}